-- LoginEffectM
-- create by huanghr Nov/28/2016
-- 登录界面光效模块

module("LoginEffectM", package.seeall);

local loginEffectTable = {};
local _init = false;

local DEFAULT_ID          = 1;

local loginEffectId = DEFAULT_ID;

-- 默认光效ID
local MAIN_EFFECT_ID      = 1922; -- 主题光效
local TITLE_EFFECT_ID     = 1920; -- 默认标题光效
local TITLE_BG_EFFECT_ID  = 1921; -- 默认标题背景光效

local CENTER_GUMBALL_EFFECT_ID = 1923; -- 默认冈布奥光效(中间)
local RIGHT_GUMBALL_EFFECT_ID  = 1925; -- 默认冈布奥右边（右边）
local LEFT_GUMBALL_EFFECT_ID   = 1924; -- 默认冈布奥右边（左边）


function loadCsv()
    if _init then return end
    _init = true;

    loginEffectTable = {};
    local login_effect_csv = get_login_effect_CSV();

    for i = 1, #login_effect_csv do
        local id = login_effect_csv[i].id;
        loginEffectTable[id] = login_effect_csv[i];

         -- 解析特效偏移
        parseActionFrameRange(loginEffectTable[id]);
    end

    login_effect_csv = {};
end

-- 解析特效偏移
function parseActionFrameRange(info)
    local offset = info.upper_off_set;

    if offset ~= nil and offset ~= "" then
        local coords = string.explode(string.sub(offset, 2, -2), ",");
        info.upper_off_set = { tonumber(coords[1]), tonumber(coords[2]) };
    else
        info.upper_off_set = { 0, 0 };
    end

    return info;
end

-- 初始化
function init()
    if not _init then
        loadCsv();
    end
end

-- 检索
function query(id, path)
    local m = loginEffectTable[id];

    if not m or not path then
        return m;
    end

    return m[path];
end

-- 检查当前时间是否在某一区间
function checkInDuration(startTime, endTime)
    if startTime == nil or endTime == nil then
        return false;
    end

    startTime = tostring(startTime);
    endTime   = tostring(endTime);

    -- 获得当前的时间，根据配表的起止时间获得正确的ID
    local curTime = TimeM.getCurrentTime();

    -- 如果配置了年份的话，按配置的年份算
    local curYear = os.date("%Y", curTime);

    -- 长度14说明已经配置了年份，没有的话需要按照当前年份来算
    local realStartTime;
    if string.len(startTime) == 14 then
        realStartTime = startTime;
    else
        -- 当前年
        realStartTime = curYear .. startTime;
    end

    -- 结束时间，长度14说明已经配置了年份，没有的话需要按照当前年份来算
    local realEndTime;
    if string.len(endTime) == 14 then
        realEndTime = endTime;
    else
        realEndTime = curYear .. endTime;
    end

    -- 当前时间
    curTime = tonumber(os.date("%Y%m%d%H%M%S", curTime));

    -- 如果开始时间大于结束时间，那么结束时间应该为第二年,
    -- 这处理方式只在没有配置没有年份的时候起效
    if string.len(endTime) ~= 14 and tonumber(realStartTime) > tonumber(realEndTime) then
        local nextYear = tonumber(curYear) + 1;
        realEndTime = tostring(nextYear) .. endTime;
    end

    if string.len(realStartTime) ~= 14  or tonumber(realStartTime) == 0 or
        tonumber(realStartTime) > curTime then
        return false;
    end

    if string.len(realEndTime) ~= 14  or tonumber(realEndTime) == 0 or
        tonumber(realEndTime) < curTime then
        return false;
    end

    return true;
end

-- 获得玩家登录光效
function getLoginEffectList()
    loginEffectId = DEFAULT_ID;

    -- 没有配置节日光效关闭，需要检查下应该播放哪种类型光效
    if not (_G["dislist"] ~= nil and _G["dislist"]["festival_effect_closed"] == 1) then
        for id, row in pairs(loginEffectTable) do
            if checkInDuration(row.start_time, row.end_time) then
                loginEffectId = id;
                break;
            end
        end
    end

    -- 主题光效
    local mainEffectId = getMainEffectId();
    -- 标题光效
    local titleId = getTitleEffectId();
    -- 标题背景光效
    local titleBgId = getTitleBgEffectId();
    -- 左边冈布奥光效
    local leftGumball = getPetEffectId("left");
    -- 右边冈布奥
    local rightGumball = getPetEffectId("right");
    -- 中间冈布奥
    local centerGumball = getPetEffectId("center");

    local effectList = {["main_id"] = mainEffectId,
        ["title_id"] = titleId,
        ["title_bg_id"] = titleBgId,
        ["left_gumball"] = leftGumball,
        ["right_gumball"] = rightGumball,
        ["center_gumball"] = centerGumball,
    };

    -- 主题背景
    local bgPath = getBgIconPath();

    if bgPath ~= nil then
        effectList["bg_path"] = bgPath;
    end

    -- 覆盖在原来bg上的背景图
    local upperBgPath = getUpperBgInfo();

    if upperBgPath ~= nil then
        effectList["upper_bg_info"] = upperBgPath;
    end

    -- 节日光效
    local festivalEffectInfo = getFestivalEffect();

    if type(festivalEffectInfo) == "table" then
        effectList["festival_effect"] = festivalEffectInfo;
    end

    return effectList;
end

-- 检查当前光效
function queryCurInfo(path)
    return query(loginEffectId, path);
end

-- 获得主题光效
function getMainEffectId()
    -- body
    local mainEffects = queryCurInfo("main_effect") or {};

    local mainEffectId = mainEffects["default"];

    -- 海外
    if isOverseaPackage() then
        mainEffectId = mainEffects["oversea"];
    end

    -- 根据语言版本
    local lang = getLang();
    if lang and mainEffects[lang] then
        mainEffectId = mainEffects[lang];
    end

    return mainEffectId or MAIN_EFFECT_ID;
end

-- 获得标题ID
function getTitleEffectId()
    -- 国内简体中文版本使用“不思议迷宫”logo
    local titleEffects = queryCurInfo("title_effect") or {};

    local titleEffectId = titleEffects["default"];

    -- 根据语言选择
    local lang = getLang();

    -- 海外非英语版本使用带副标题logo
    if isOverseaPackage() then
        titleEffectId = titleEffects["oversea"];

        -- 海外版本再细分语言（英文版的另外处理）
        if lang and lang ~= "en" and titleEffects[lang] then
            titleEffectId = titleEffects[lang];
        end
    end

    -- 英文版
    if lang and lang == "en" and titleEffects[lang] then
        titleEffectId = titleEffects[lang];
    end

    return titleEffectId or TITLE_EFFECT_ID;
end

-- 获得标题背景光效ID
function getTitleBgEffectId()
     -- 国内简体中文版本使用“不思议迷宫”logo
    local titleBgIds = queryCurInfo("title_bg") or {};

    local titleBgEffectId = titleBgIds["default"];

    -- 根据语言选择
    local lang = getLang();

    -- 海外非英语版本使用带副标题logo
    if isOverseaPackage() then
        titleBgEffectId = titleBgIds["oversea"];

        -- 海外版本再细分语言（英文版的英文版的下面再处理）
        if lang and lang ~= "en" and titleBgIds[lang] then
            titleBgEffectId = titleBgIds[lang];
        end
    end

    -- 英文版本单独处理
    if lang and lang == "en" and titleBgIds[lang] then
        titleBgEffectId = titleBgIds[lang];
    end

    return titleBgEffectId or TITLE_BG_EFFECT_ID;
end

-- 获得标题冈布奥光效
function getPetEffectId(posType)
    local effectId;
    if posType == "left" then
        effectId = queryCurInfo("left_gumball");
        if effectId == "" or effectId == nil then
            effectId = LEFT_GUMBALL_EFFECT_ID;
        end
    elseif posType == "right" then
        effectId = queryCurInfo("right_gumball");
        if effectId == "" or effectId == nil then
            effectId = RIGHT_GUMBALL_EFFECT_ID;
        end
    else
        effectId = queryCurInfo("center_gumball");

        if effectId == "" or effectId == nil then
            effectId = CENTER_GUMBALL_EFFECT_ID;
        end
    end

    return effectId;
end

-- 获得下节日背景，没有返回nil
function getBgIconPath()
    local loginBgFile = queryCurInfo("login_bg");

    if loginBgFile == nil or loginBgFile == "" then
        return nil;
    end

    local fullName = getLoginFestivalBgIconPath(loginBgFile);

    -- 不存在，不需要加载
    if fullName == nil then
        return nil;
    end

    return fullName;
end

-- 获得覆盖背景图bg
function getUpperBgInfo()
    local upperBg = queryCurInfo("upper_bg");

    if  upperBg == nil or upperBg == "" then
        return nil;
    end

    local fullName = getLoginFestivalUpperIconPath(upperBg);

    -- 不存在，不需要加载
    if fullName == nil then
        return nil;
    end

    local offset = queryCurInfo("upper_off_set") or {};
    local upperBgInfo = {};
    upperBgInfo["path"] = fullName;
    upperBgInfo["offset_x"] = offset[1] or 0;
    upperBgInfo["offset_y"] = offset[2] or 0;

    return upperBgInfo;
end

-- 获得节日光效
function getFestivalEffect()
    local festivalEffect = queryCurInfo("festival_effect");
    if type(festivalEffect) ~= "number" then
        return nil;
    end

    local appearFrame = queryCurInfo("appear_frame");

    -- 出现帧数默认第一帧
    if tonumber(appearFrame) == 0 then
        appearFrame = 1;
    end

    local festivalEffectInfo = {};

    festivalEffectInfo["effect_id"] = festivalEffect;
    festivalEffectInfo["appear_frame"] = appearFrame;

    -- 循环开始
    local firstFrame = queryCurInfo("loop_first_frame");

    -- 循环最后帧
    local lastFrame = queryCurInfo("loop_last_frame");

    -- 最后一帧必须大于开始帧
    if type(firstFrame) == "number" and type(lastFrame) == "number" and lastFrame > firstFrame then
        festivalEffectInfo["first_frame"] = firstFrame;
        festivalEffectInfo["last_frame"] = lastFrame;
    end

    if queryCurInfo("festival_in_top") == 1 then
        festivalEffectInfo["festival_in_top"] = true;
    else
        festivalEffectInfo["festival_in_top"] = false;
    end

    return festivalEffectInfo;
end